'use strict';
const SortWord = require('sort-word')
const Controller = require('egg').Controller;

class FriendController extends Controller {
  // 通讯录列表
	async list(){
		const { ctx, app } = this;
		let current_user_id = ctx.authUser.id;
		// 获取并统计我的好友
		let friends = await app.model.Friend.findAndCountAll({
				where: {
						user_id: current_user_id
				},
				include: [{
						model: app.model.User,
						as: "friendInfo",
						attributes: ['id', 'username', 'nickname', 'avatar']
				}]
		});
		// friends.rows获取到的数据
		let res = friends.rows.map(item => {
				// 用户的nickname
				let name = item.friendInfo.nickname ? item.friendInfo.nickname : item.friendInfo.username;
				// 备注名
				if (item.nickname) {
						name = item.nickname
				}
				return {
						id: item.id,
						user_id: item.friendInfo.id,
						name,
						username: item.friendInfo.username,
						avatar: item.friendInfo.avatar
				}
		});

		// 排序 res=[{},{}]，通过name排序
		if (res.length > 0) {
				friends.rows = new SortWord(res, 'name');
		}

		ctx.apiSuccess(friends);
	}
	
	// 查看用户资料
	async read(){
		const { ctx, app } = this;
		let current_user_id = ctx.authUser.id;
		// 用户id
		let id = ctx.params.id ? parseInt(ctx.params.id) : 0;
		let user = await app.model.User.findOne({
			where: { id, status: 1 },
			attributes: { exclude: ['password'] },
			include: [{
				model: app.model.Moment,
				order: [[
					'id', 'desc'
				]],
				limit: 1
			}]
		})
		let friend = await app.model.Friend.findOne({
			where: {
				friend_id: id,
				user_id: current_user_id
			},
			include:[{
				model: app.model.Tag,
				attributes:{
					include:['name']
				}
			}]
		});
		if(!user){
			ctx.throw(400, '用户不存在！')
		}
		
		let res = {
			id: user.id,
			username: user.username,
			nickname: user.nickname ? user.nickname : user.username,
			avatar: user.avatar,
			sign: user.sign,
			area: user.area,
			friend: false,
			moments: user.moments
		}
		if(friend) {
			res.friend = true
			// 已备注就用备注名
			if(friend.nickname) res.nickname = friend.nickname
			res = {
				...res,
				lookme: friend.lookme,
				lookhim: friend.lookhim,
				star: friend.star,
				isblack: friend.isblack,
				// 返回标签名
				tags: friend.tags.map(item => item.name),
				
			}
		}
		ctx.apiSuccess(res)
	}

	// 移入/移除黑名单
	async setblack(){
		const { ctx,app } = this
		let current_user_id = ctx.authUser.id;
		let id = ctx.params.id ? parseInt(ctx.params.id) : 0
		// 参数验证
		ctx.validate({
			isblack:{
				type: 'int',
				range: {
					in: [0,1]
				},
				required: true,
				desc: '移入或移除黑名单'
			}
		})
		
		let friend = await app.model.Friend.findOne({
			where: {
				friend_id: id,
				user_id: current_user_id
			}
		})
		if(!friend) ctx.throw(400, '该记录不存在');
		// 修改记录
		friend.isblack = ctx.request.body.isblack
		await friend.save()
		ctx.apiSuccess('操作成功')
	}

	// 设为星标好友
	async setstar(){
		const { ctx, app } = this
		const current_user_id = ctx.authUser.id
		let id = ctx.params.id ? parseInt(ctx.params.id) : 0;
		// star：1或0，0取消星标
		ctx.validate({
			star: {
				type: 'int',
				required: true,
				range:{
					in:[0, 1]
				},
				desc: '设置/取消好友星标'
			}
		})
		// 查询好友表(用户和好友关系)记录
		let friend = await app.model.Friend.findOne({
			where: {
				friend_id: id,
				user_id: current_user_id
			}
		})
		if(!friend) ctx.throw(400, '该记录不存在！')
		friend.star = ctx.request.body.star
		await friend.save()
		ctx.apiSuccess('设置成功')
	}

	// 设置朋友圈权限
	async setMomentAuth(){
		const { ctx, app } = this
		const current_user_id = ctx.authUser.id
		let id = ctx.params.id ? parseInt(ctx.params.id) : 0;
		ctx.validate({
			lookme: {
				type: 'int',
				required: true,
				range:{
					in:[0, 1]
				}
			},
			lookhim: {
				type: 'int',
				required: true,
				range:{
					in:[0, 1]
				}
			}
		})
		let friend = await app.model.Friend.findOne({
			where: {
				user_id: current_user_id,
				friend_id: id,
				isblack: 0
			}
		})
		if(!friend) ctx.throw(400, '不存在记录！')
		let { lookme, lookhim } = ctx.request.body
		friend.lookhim = lookhim
		friend.lookme = lookme
		friend.save()
		ctx.apiSuccess('ok')
	}

	// 设置备注和标签
	async setremarkTag(){
		const { ctx, app } = this
		const current_user_id = ctx.authUser.id
		let id = ctx.params.id ? parseInt(ctx.params.id) : 0;
		// 参数验证
		ctx.validate({
			nickname: {
					type: 'string',
					required: false,
					desc: "昵称"
			},
			tags: {
					type: 'string',
					required: true,
					desc: "标签"
			},
		});
		// 查看好友是否存在
		let friend = await app.model.Friend.findOne({
			where: {
					user_id: current_user_id,
					friend_id: id,
					isblack: 0
			},
			include: [{
					model: app.model.Tag
			}]
		});
		// return ctx.apiSuccess(friend)
		if (!friend) {
				ctx.throw(400, '该记录不存在');
		}
		let { tags, nickname } = ctx.request.body;
		// 设置备注
		friend.nickname = nickname;
		await friend.save();
		// 获取当前用户所有标签
		let allTags = await app.model.Tag.findAll({
				where: {
						user_id: current_user_id
				}
		});
		// 当前用户所有标签名字
		let allTagsName = allTags.map(item => item.name);
		// 新标签名字
		let newTags = tags.split(',');
		// 需要添加的标签(去除新标签里的旧标签)
		let addTags = newTags.filter(item => !allTagsName.includes(item));
		// 组织批量插入数组
		addTags = addTags.map(name => {
			return {
					name,
					user_id: current_user_id
			}
		});
		// 写入tag表
		let resAddTags = await app.model.Tag.bulkCreate(addTags);
		// 找到新标签id
		newTags = await app.model.Tag.findAll({
			where: {
					user_id: current_user_id,
					// 数组
					name: newTags
			}
		});
		let newTagsIds = newTags.map(item => item.id);
		// 旧标签id
		let oldTagsIds = friend.tags.map(item => item.id);
		// 旧标签没有新标签有的
		let addTagsIds = newTagsIds.filter(id => !oldTagsIds.includes(id));
		// 旧标签有新标签没有的
		let delTagsIds = oldTagsIds.filter(id => !newTagsIds.includes(id));
		// 组织数据
		addTagsIds = addTagsIds.map(tag_id => {
				return {
						tag_id,
						friend_id: friend.id
				}
		});
		// 添加关联关系，批量添加(中间表)
		app.model.FriendTag.bulkCreate(addTagsIds);
		// 删除关联关系，注：这里没有处理tag表，tag表还有原来标签数据
		app.model.FriendTag.destroy({
				where: {
						// 数组
						tag_id: delTagsIds,
						friend_id: friend.id
				}
		});
		ctx.apiSuccess('ok')
	}

	// 删除好友
	async destroy(){
		const { ctx, app } = this;
		let current_user_id = ctx.authUser.id;

		ctx.validate({
			friend_id: {
				type: "int",
				required: true,
				desc: "好友id"
			}
		});
		let { friend_id } = ctx.request.body;
		await app.model.Friend.destroy({
			where: {
				user_id: current_user_id,
				friend_id
			}
		});

		ctx.apiSuccess('ok');

		app.model.Friend.destroy({
			where: {
				user_id: friend_id,
				friend_id: current_user_id
			}
		});

		this.deleteTimeLineMoment(friend_id, current_user_id);
		this.deleteTimeLineMoment(current_user_id, friend_id);

		// 删除apply表对应数据
		app.model.Apply.destroy({
			where: {
				user_id: current_user_id,
				friend_id: friend_id
			}
		});
		app.model.Apply.destroy({
			where: {
				user_id: friend_id,
				friend_id: current_user_id
			}
		});
	}
	
	// 删除非好友的朋友圈时间轴
	async deleteTimeLineMoment(friend_id, current_user_id){
		const { app, ctc } = this;
		// 拿到好友发布的朋友圈id
		let moments = await app.model.Moment.findAll({
			where: {
				user_id: friend_id
			},
			attributes: ['id']
		});
		moments = moments.map(item => item.id)
		await app.model.MomentTimeline.destroy({
			where: {
				user_id: current_user_id,
				moment_id: moments
			}
		})
	}

}

	

module.exports = FriendController;
